/**
 * 
 */
package com.shine.icis.dao.imp;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.sql.hibernate.CommQuery;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.dao.IAccrualInfoDAO;
import com.shine.icis.dao.imp.BaseHibernateDAO;
import com.shine.icis.hbean.AccrualInfo;

/**
 * @author zsy
 *
 */
public class AccrualInfoDAO extends BaseHibernateDAO implements IAccrualInfoDAO {

	@Override
	public AccrualInfo findById(long id) {
		AccrualInfo sl = (AccrualInfo)getSession().get(AccrualInfo.class, id);
		return sl;
	}
	
	@Override
	public boolean checkId(int tradeDate, String arapType, String oppPar, String mktCodeSipf, 
			String arapBusiType, String curTypeSipf, int harapDate, int busiDate) {
		Query query = getSession().createQuery("select count(a) from AccrualInfo as a where " +
				"a.tradeDate=:tradeDate and a.arapType=:arapType and a.oppPar=:oppPar and " +
				"a.mktCodeSipf=:mktCodeSipf and a.arapBusiType=:arapBusiType and " +
				"a.curTypeSipf=:curTypeSipf and a.harapDate=:harapDate and a.busiDate=:busiDate");
		query.setInteger("tradeDate", tradeDate);
		query.setString("arapType", arapType);
		query.setString("oppPar", oppPar);
		query.setString("mktCodeSipf", mktCodeSipf);
		query.setString("arapBusiType", arapBusiType);
		query.setString("curTypeSipf", curTypeSipf);
		query.setInteger("harapDate", harapDate);
		query.setInteger("busiDate", busiDate);
		Long count = (Long)query.uniqueResult();
		if (count != null && count.longValue() > 0) {
			return false;
		}
		return true;
	}

	@Override
	public ListPage<AccrualInfo> findListPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}

		try {
			StringBuffer sbQueryString = new StringBuffer();			
			sbQueryString.append("select ai from AccrualInfo as ai where 1=1");
			if(qp.getParameter("busiDate") != null){
				sbQueryString.append(" and ai.busiDate = :busiDate");
			}
			
			if(qp.getParameter("arapType") != null){
				sbQueryString.append(" and ai.arapType = :arapType");
			}
			
			return new CommQuery<AccrualInfo>().queryListPage(qp, 
					qp.appendOrders(sbQueryString, "ai" ), getSession());
		} catch(RuntimeException re) {
			re.printStackTrace();
			return new  ListPage<AccrualInfo>();
		}
	}
	
	@Override
	public ListPage<AccrualInfo> findStatListPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		try {
			Query query = getSession().getNamedQuery("AccrualInfoDAO.findStatListPage");
			Query countQuery = getSession().getNamedQuery("AccrualInfoDAO.findStatListPage_count");
			ListPage<Object> page = new CommQuery<Object>().queryListPage(qp, query, countQuery);
			ListPage<AccrualInfo> accPage = new ListPage<AccrualInfo>();
			accPage.setTotalCount(page.getTotalCount());
			accPage.setCurrentPageNo(page.getCurrentPageNo());
			accPage.setCurrentPageSize(page.getCurrentPageSize());
			if (page != null && page.getDataList() != null) {
				List<AccrualInfo> datas = new ArrayList<AccrualInfo>();
				for (Object o : page.getDataList()) {
					Object[] oArr = (Object[])o;
					AccrualInfo accr = new AccrualInfo();
					accr.setId((Long)oArr[0]);
					accr.setTradeDate((Integer)oArr[1]);
					accr.setArapType((String)oArr[2]);
					accr.setOppPar((String)oArr[3]);
					accr.setMktCodeSipf((String)oArr[4]);
					accr.setArapBusiType((String)oArr[5]);
					accr.setCurTypeSipf((String)oArr[6]);
					accr.setHarapDate((Integer)oArr[7]);
					accr.setBusiDate((Integer)oArr[8]);
					accr.setArapReason((String)oArr[9]);
					accr.setArapAmt((Double)oArr[10]);
					accr.setArapDate((Integer)oArr[11]);
					accr.setSummary((String)oArr[12]);
					datas.add(accr);
				}
				accPage.setDataList(datas);
			}
			return accPage;
		} catch(RuntimeException re) {
			re.printStackTrace();
			return new  ListPage<AccrualInfo>();
		}
	}
}
